
/*
 * CS600 Homework #1 
 * - Insertion Sort (Always ascending case)
 * by Shoufu Luo, 2/02/2012
 *
 */

#include "sort.h"
#include "insert.h"

#if 0
void
insert_sort(hw1type *A, int len)
{
	int		i, j;
	hw1type	key;
	
	for (i = 1; i < len; i++)
	{
		assign(key, A[i]);
		for (j = i - 1; j >= 0; j--)  //backward
		{
			if (compare(key, A[j]) == -1)// ascending && key < a[j]
			{
				assign(A[j + 1], A[j]); //move this to next
				continue;
			}
			break;
		}
		assign(A[j + 1], key); // put key here	
	}
}
#else
void
insert_sort(hw1type *A, int l, int r)
{
	int		i, j;
	hw1type	key;
	
	for (i = l + 1; i <= r; i++)
	{
		assign(key, A[i]);
		for (j = i - 1; j >= l; j--)  //backward
		{
			if (compare(key, A[j]) == -1)// ascending && key < a[j]
			{
				assign(A[j + 1], A[j]); //move this to next
				continue;
			}
			break;
		}
		assign(A[j + 1], key); // put key here	
	}
}
#endif
